package cn.lingyangwl.agile.infra.gen.template;

import cn.hutool.core.util.StrUtil;
import cn.lingyangwl.agile.infra.gen.utils.FieldUtil;
import com.github.yitter.idgen.YitIdHelper;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;
import java.util.UUID;

/**
 * tmpl中的上下文
 * 最终会把SQL上下文信息放到velocity中
 * @author shenguangyang
 */
@Getter
@Setter
public class TmplContent {
    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss");

    private final LocalDateTime localDateTime = LocalDateTime.now();

    /**
     * 表结构定义
     */
    private TmplTableDefinition tableDefinition;

    private TmplJavaColumnDefinition javaPkColumn;
    /**
     * 包名
     */
    private String packageName;

    /**
     * 包的子路径
     */
    private String packageSubPath;

    /**
     * 删除的前缀
     */
    private String delPrefix;

    /**
     * 数据库名
     */
    private String dbName;

    /**
     * 生成模块名
     */
    private String moduleName;

    /**
     * 说明
     */
    private String description;

    /**
     * 生成功能名
     */
    private String functionName;

    /**
     * 作者名
     */
    private String author;

    /**
     * 版本号
     */
    private String version;

    /**
     * 类名
     */
    private String className;

    public void setTableDefinition(TmplTableDefinition tableDefinition) {
        this.tableDefinition = tableDefinition;
        this.javaPkColumn = this.tableDefinition.getPkColumn();
    }

    public String getDatetime() {
        return localDateTime.format(DATE_TIME_FORMATTER);
    }

    public String getDate() {
        return localDateTime.format(DATE_FORMATTER);
    }

    public String getTime() {
        return localDateTime.format(TIME_FORMATTER);
    }

    public int getRandomInt() {
        return RandomUtils.nextInt();
    }

    public long getRandomLong() {
        return RandomUtils.nextLong();
    }

    public boolean getRandomBoolean() {
        return RandomUtils.nextBoolean();
    }

    public String getUuid() {
        return UUID.randomUUID().toString();
    }

    public long getNextId() {
        return YitIdHelper.nextId();
    }

    /**
     * 返回Java类名
     */
    public String getJavaBeanName() {
        return getClassName();
    }

    /**
     * 返回类名
     */
    public String getClassName() {
        return this.className;
    }

    /**
     * 返回Java类名全小写
     */
    public String getJavaBeanNameL() {
        return this.className.toLowerCase();
    }

    /**
     * java类名首字母小写
     */
    public String getJavaBeanNameLF() {
        return StrUtil.lowerFirst(this.className);
    }

    /**
     * 返回Java类名驼峰转横杠
     */
    public String getJavaBeanNameHB() {
        String tableName = tableDefinition.getTableName();
        if(delPrefix != null){
            String[] split = delPrefix.split("\\s*,\\s*");
            for (String prefix : split){
                tableName = StringUtils.removeStart(tableName, prefix);
            }
        }

        tableName = tableName.replace("_","-");
        tableName = FieldUtil.dotFilter(tableName);
        return tableName;
    }

    public String getPkName() {
        if (javaPkColumn != null) {
            return javaPkColumn.getColumnName();
        }
        return "";
    }

    public String getJavaPkName() {
        if (javaPkColumn != null) {
            return javaPkColumn.getJavaFieldName();
        }
        return "";
    }

    public String getJavaPkType() {
        if (Objects.nonNull(javaPkColumn)) {
            return javaPkColumn.getJavaType();
        }
        return "";
    }

    public String getMybatisPkType() {
        if (Objects.nonNull(javaPkColumn)) {
            return javaPkColumn.getMybatisJdbcType();
        }
        return "";
    }
}
